package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.MenuDataDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class MenuDataServiceImpl implements MenuDataService {
    @Autowired
    private MenuDataDao menuDataDao;
    @Transactional
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
      log.info("[菜单-查询]data:{}",queryPageBean);

        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询page
        Page<Menu> page= menuDataDao.selectByCondition(queryPageBean.getQueryString());
        log.info("[查数据]data:{}",page);
        //返回数据
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Menu menu, Integer menuDataId) {
        log.info("[菜单-新增]data:{},menuDataId:{}",menu,menuDataId);
        menuDataDao.add(menu);
    }

    @Override
    public Menu selectById(Integer id) {

        /*
        * 根基ID查询
        * */
        return menuDataDao.selectById(id);
    }

    @Override
    public void edit( Menu menu) {
        log.info("[菜单-更新]data:{}",menu);
         menuDataDao.update(menu);

    }

    @Transactional
    @Override
    public void delete(Integer id) {
        log.info("[菜单-删除]data:{}",id);
        List<Menu> menus = menuDataDao.guanlian(id);
            //判断是否有子类菜单
        for (Menu menu : menus) {
            if (menu.getLevel()=="1"){
                throw new BusinessRuntimeException("当前菜单下有子菜单 ,无法删除");
        }

        }
        menuDataDao.deleteById(id);
    }
}
